package com.banciyuan.bcywebview.base.helper.data;

import android.content.Context;

import com.banciyuan.bcywebview.utils.constants.MyConstants;

import de.greenrobot.daoexample.dao.DaoMaster;
import de.greenrobot.daoexample.dao.DaoMaster.OpenHelper;
import de.greenrobot.daoexample.dao.DaoSession;


public class DataBaseHelper {

    private DaoSession daoSession;
    private DaoMaster daoMaster;
    public final Object databaseLock = new Object();

    /**
     * 取得DaoMaster
     *
     * @param context
     * @return DaoMaster
     */
    public DaoMaster getDaoMaster(Context context) {
        synchronized (databaseLock) {
            if (daoMaster == null) {
                OpenHelper helper = new DaoMaster.DevOpenHelper(context, MyConstants.DB_NAME, null);
                daoMaster = new DaoMaster(helper.getWritableDatabase());
            }
            return daoMaster;
        }
    }

    /**
     * 取得DaoSession
     *
     * @param context
     * @return DaoSession
     */
    public DaoSession getDaoSession(Context context) {
        synchronized (databaseLock) {
            if (daoSession == null) {
                if (daoMaster == null) {
                    daoMaster = getDaoMaster(context);
                }
                daoSession = daoMaster.newSession();
            }
            return daoSession;
        }
    }
}